/**
 * 
 */
package com.hson.demo.base.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import com.hson.demo.base.dto.PageModel;


/**
 * 只读数据源
 * 
 * @author Hungel zou
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public abstract class AbstractReadOnlyBaseDao {

	// 只读数据源
	@Autowired
	protected NamedParameterJdbcTemplate namedParameterJdbcTemplate_readonly;
	@Autowired
	protected JdbcTemplate jdbcTemplate_readonly;
	
	/**
	 * get po by id
	 * 
	 * @param <T>
	 * @param strSql
	 * @param id
	 * @param t
	 * @return
	 */
	public <T> T getPo(String strSql, Long id, T t){
		List<T> resultList = jdbcTemplate_readonly.query(strSql, new Object[]{id}, new BeanPropertyRowMapper(t.getClass()));
		if(resultList != null && resultList.size() > 0){
			return resultList.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * get page list
	 * @param pageNum start from 1
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @param rowMapper
	 * @return
	 */
	public PageModel getPageList(Integer pageNum, Integer itemCount, String sql, Object[] sqlParams, RowMapper rowMapper){
		return getPageList(pageNum, itemCount, sql, sqlParams, null, null, rowMapper);	
	}
	
	/**
	 * get page list
	 * @param pageNum start from 1
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @param orderField
	 * @param orderType
	 * @param rowMapper
	 * @return
	 */
	public PageModel getPageList(Integer pageNum, Integer itemCount, String sql, Object[] sqlParams, String orderField,String orderType, RowMapper rowMapper){
		
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}
		if (itemCount == null || itemCount <= 0) {
			itemCount = 10;
		}
		
		//get the total count sql
		String getTotalSql = "SELECT COUNT(0) FROM (" + sql + ") V";
		
		//get the records sql
		if(StringUtils.isNotEmpty(orderField)){
			sql += " ORDER BY " + orderField + " " + orderType;
		}
		
		String getResultSql = buildPageSql(Integer.toString(pageNum), Integer.toString(itemCount), sql);
		
		int totalCount = jdbcTemplate_readonly.queryForInt(getTotalSql, sqlParams);
		
		List resultList = null;
		if(rowMapper == null){
			resultList =  jdbcTemplate_readonly.queryForList(getResultSql, sqlParams);
		}else{
			resultList =  jdbcTemplate_readonly.query(getResultSql, sqlParams, rowMapper);
		}
		
		PageModel pageModel = new PageModel(pageNum, itemCount, totalCount, resultList);
		
        return pageModel;
	
	}
	
	/**
	 * TODO oracle分页需要按照oracle方式处理
	 */
	protected String buildPageSql(String pageId, String itemCount,String strSql) {
		StringBuffer ret = new StringBuffer("select * from (select a.*, rownum rn from ( ");
		ret.append(strSql);
		ret.append(" ) a where rownum <= ?) where rn > ? ");
		return ret.toString();
	}
	
	protected String buildPageSql(String strSql) {
		StringBuffer ret = new StringBuffer("select * from (select a.*, rownum rn from ( ");
		ret.append(strSql);
		ret.append(" ) a where rownum <= ?) where rn > ? ");
		return ret.toString();
	}
	
}
